package com.example.pc.adaptivelandscapeandfagmentationvalues;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class LeftFragment extends Fragment implements AdapterView.OnItemClickListener {


    private ListView lv_items;

    public LeftFragment() {
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_left, container, false);
    }
    ArrayList<String> list = null;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lv_items = getView().findViewById(R.id.lv_items);
        list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("item" + i);
        }//
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, list);
        lv_items.setAdapter(arrayAdapter);
        lv_items.setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String s = list.get(position);
        String content = "这是内容啊:" + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s;
        //当前屏幕方向
        if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //TODO 将s 传递给 RightFragment  横屏将执行的
            RightFragment right = (RightFragment) getFragmentManager().findFragmentByTag("right");
            right.seAtrticle(s, content);
        } else {//竖屏将执行的
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            RightFragment fragment = new RightFragment();
            Bundle bundle = new Bundle();
            bundle.putString("title", s);
            bundle.putString("content", content);
            //绑定数据
            fragment.setArguments(bundle);
            ft.replace(R.id.fl_left, fragment);
            ft.addToBackStack(null);//是的 ,按back按键,可以回到上一个事务的状态
            ft.commit();
        }
    }
}
